package com.xuecheng.manage_cms.service.impl;

import com.xuecheng.framework.domain.cms.CmsTemplate;
import com.xuecheng.framework.domain.cms.request.QueryTemplateRequest;
import com.xuecheng.framework.domain.cms.response.CmsTemplateCode;
import com.xuecheng.framework.domain.cms.response.TemplateResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.dao.CmsTemplateRepository;
import com.xuecheng.manage_cms.service.CmsTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CmsTemplateServiceImpl implements CmsTemplateService {

    @Autowired
    CmsTemplateRepository cmsTemplateRepository;

    @Override
    public QueryResponseResult findList(int page, int size, QueryTemplateRequest templateRequest) {
        if(templateRequest == null){
            templateRequest = new QueryTemplateRequest();
        }
        CmsTemplate cmsTemplate = new CmsTemplate();
        if(StringUtils.isNotEmpty(templateRequest.getSiteId())){
            cmsTemplate.setSiteId(templateRequest.getSiteId());
        }
        if(StringUtils.isNotEmpty(templateRequest.getTemplateName())){
            cmsTemplate.setTemplateName(templateRequest.getTemplateName());
        }
        if(StringUtils.isNotEmpty(templateRequest.getTemplateParameter())){
            cmsTemplate.setTemplateParameter(templateRequest.getTemplateParameter());
        }
        //构建条件对象
        ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("templateName", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<CmsTemplate> example = Example.of(cmsTemplate, matcher);
        //构建分页参数
        if(page <= 0){
            page = 1;
        }
        page -= 1;
        if(size <= 0){
            size = 10;
        }
        Pageable pageable = PageRequest.of(page, size);
        //查询
        Page<CmsTemplate> pageData = cmsTemplateRepository.findAll(example, pageable);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(pageData.getContent());
        queryResult.setTotal(pageData.getTotalElements());
        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
        return queryResponseResult;
    }

    @Override
    public TemplateResult add(CmsTemplate cmsTemplate) {
        //根据站点ID和模板名称唯一
        CmsTemplate dbcmstemplate = cmsTemplateRepository.findBySiteIdAndTemplateName(cmsTemplate.getSiteId(), cmsTemplate.getTemplateName());
        if(null != dbcmstemplate){
            //已经存在
            ExceptionCast.cast(CmsTemplateCode.CMS_ADDTEMPLATE_EXISTS);
        }
        //将ID置空
        cmsTemplate.setTemplateId(null);
        cmsTemplateRepository.save(cmsTemplate);
        return new TemplateResult(CommonCode.SUCCESS,cmsTemplate);
    }

    @Override
    public CmsTemplate findById(String id) {
        Optional<CmsTemplate> optional = cmsTemplateRepository.findById(id);
        if(optional.isPresent()){
            CmsTemplate cmsTemplate = optional.get();
            return cmsTemplate;
        }
        return null;
    }

    @Override
    public TemplateResult edit(String id, CmsTemplate cmsTemplate) {
        //查询此ID的记录是否存在
        CmsTemplate one = this.findById(id);
        if(null == one){
            ExceptionCast.cast(CmsTemplateCode.CMS_EDITTEMPLATE_NOTEXISTS);
        }
        one.setSiteId(cmsTemplate.getSiteId());
        one.setTemplateName(cmsTemplate.getTemplateName());
        one.setTemplateParameter(cmsTemplate.getTemplateParameter());
        one.setTemplateFileId(cmsTemplate.getTemplateFileId());
        cmsTemplateRepository.save(one);
        return new TemplateResult(CommonCode.SUCCESS,cmsTemplate);
    }

    @Override
    public ResponseResult delete(String id) {
        cmsTemplateRepository.deleteById(id);
        return new QueryResponseResult(CommonCode.SUCCESS,null);
    }

}
